package org.gordonframework.site.transitions {
	import org.gordonframework.core.events.GProcessEvent;
	import org.gordonframework.core.tween.GTween;
	import org.gordonframework.site.transitions.GTransition;

	import flash.geom.Rectangle;	

	/**
	 * @author sampet
	 */
	public class GSlide extends GTransition {

		private var inDuration:Number;
		private var outDuration:Number;

		private var tweeningOut:Boolean;

		public var direction:String;

		public function GSlide(direction:String = "leftRight", inDuration:Number = 0.5, outDuration:Number = 0.5) {
			this.outDuration = outDuration;
			this.inDuration = inDuration;
			this.direction = direction;
			this.scrollRect = new Rectangle( 0, 0, 320, 240 );
		}

		override protected function hideView():void {
			if(!currentView) {
				hideViewComplete( );
			}
			else if(!tweeningOut) {
				tweeningOut = true;
				var property:Object;
				switch(direction) {
					case GSlideDirection.LEFT :
					case GSlideDirection.RIGHT_LEFT :
						property = {x:0 - scrollRect.width};
						break;
					case GSlideDirection.RIGHT :					case GSlideDirection.LEFT_RIGHT :
						property = {x:scrollRect.width};
						break;
					case GSlideDirection.TOP :
					case GSlideDirection.BOTTOM_TOP :
						property = {y:0 - scrollRect.height};
						break;
					case GSlideDirection.BOTTOM :
					case GSlideDirection.TOP_BOTTOM :
						property = {y:scrollRect.height};
						break;									
				}
				var tween:GTween = new GTween( currentView, property, outDuration * durationMultiplier, currentView.outro( durationMultiplier ) );
				tween.addEventListener( GProcessEvent.COMPLETE, tweenOutCompleteHandler );
				tween.start( );
			}
		}

		private function tweenOutCompleteHandler(event:GProcessEvent):void {
			tweeningOut = false;
			currentView.visible = false;
			hideViewComplete( );
		}

		override protected function showView():void {
			var property:Object;
			switch(direction) {
				case GSlideDirection.LEFT :
				case GSlideDirection.LEFT_RIGHT :
					currentView.x = 0 - scrollRect.width;					currentView.y = 0;
					property = {x:0};
					break;
				case GSlideDirection.RIGHT :
				case GSlideDirection.RIGHT_LEFT :
					currentView.x = scrollRect.width;
					currentView.y = 0;
					property = {x:0};
					break;
				case GSlideDirection.TOP :
				case GSlideDirection.TOP_BOTTOM :
					currentView.x = 0;
					currentView.y = 0 - scrollRect.height;
					property = {y:0};
					break;
				case GSlideDirection.BOTTOM :
				case GSlideDirection.BOTTOM_TOP :
					currentView.x = 0;
					currentView.y = scrollRect.height;
					property = {y:0};
					break;										
			}
			currentView.visible = true;
			var tween:GTween = new GTween( currentView, property, inDuration * durationMultiplier );
			tween.addEventListener( GProcessEvent.COMPLETE, tweenInCompleteHandler );
			tween.start( );
		}

		private function tweenInCompleteHandler(event:GProcessEvent):void {
			currentView.intro( durationMultiplier );
			showViewComplete( );
		}
	}
}
